//index.js
//获取应用实例
const app = getApp()
var indexData = require("../indexdata/indexdata.js");
// const util = require('../../../utils/util.js');
var tabBar = require('../template/tabBar.js');
var inviteHomedata = require('../inviteHomedata/inviteHomedata.js')
var num = 0;
var sortRule1 = true; // 价格
var sortRule2 = true; // 销量
var sortClick1 = 2;
var sortClick2 = 2;
var twoBtn = false; //单列多列
Page({
  data: {
    currentTab: 0,
    winHeight: 0,
    winWidth: 0,
    bannerImg: [ //轮播图片
      app.globalData.url + "TsdXcx/img/banner/" + app.globalData.comid + "/" + app.globalData.nzid + "banner1.jpg",
      app.globalData.url + "TsdXcx/img/banner/" + app.globalData.comid + "/" + app.globalData.nzid + "banner2.jpg",
      app.globalData.url + "TsdXcx/img/banner/" + app.globalData.comid + "/" + app.globalData.nzid + "banner3.jpg"
    ],
    classif: [ //分类
      {
        "img": "../images/inviteHome/dancigou.png",
        "flxh": "0",
        "text": "单日购买"
      },
      {
        "img": "../images/inviteHome/xinxianshucai.png",
        "flxh": "1",
        "text": "新鲜蔬菜"
      },
      {
        "img": "../images/inviteHome/shilingshuiguo.png",
        "flxh": "2",
        "text": "时令水果"
      },
      {
        "img": "../images/inviteHome/shuichendongpin.png",
        "flxh": "3",
        "text": "水产冻品"
      },
      {
        "img": "../images/inviteHome/rouqin.png",
        "flxh": "4",
        "text": "肉禽蛋品"
      },
      {
        "img": "../images/inviteHome/liangyoutiaowen.png",
        "flxh": "5",
        "text": "粮油调味"
      },
      {
        "img": "../images/inviteHome/xiuxianshipin.png",
        "flxh": "6",
        "text": "休闲食品"
      },
      {
        "img": "../images/inviteHome/jvjiabaihuo.png",
        "flxh": "7",
        "text": "居家百货"
      },
      {
        "img": "../images/inviteHome/shenghuofuwu.png",
        "flxh": "8",
        "text": "生活服务"
      },
      {
        "img": "../images/inviteHome/qita.png",
        "flxh": "9",
        "text": "其他"
      },
    ],
    sortClick1: 1,
    sortClick2: 1,
    twoBtn: false,
    sortImg: {
      "sort1": "../images/index/sort1.png",
      "sort2": "../images/index/sort2.png",
      "sort3": "../images/index/sort3.png",
      "two": "../images/index/two.png", //两列
      "one": "../images/index/one.png" //单行
    },
    usedata: "", //产品列表信息
    usedata2: "", //产品列表信息
    shopSumText: "",
    navbarInitTop: 0, //导航栏初始化距顶部的距离
    isFixedTop: false, //是否固定顶部
    flag: false,
    shoppingNumber: '3',
    newsid: null,
    productImg: null,
    syspars: "",
    inviteHomedata:'',//团长信息
    classifSellers: [ //热卖分类
      {
        "flxh": "0",
        "text": "热卖商品"
      },
      {
        "flxh": "1",
        "text": "包邮到家"
      },
      {
        "flxh": "2",
        "text": "生活服务"
      },
      {
        "flxh": "3",
        "text": "网红新品"
      },
    ],
    elasticFrameImg: "../../../pages/images/index/tankuang.png",//20200312弹框图
    elasticFrameBtn: false,
    tabBar:'',
  },
  onLoad: function () {
    var that = this;
    this.setData({
      //推荐消息， 订奶列表数据
      usedata: indexData.setdata,
      messageRolling: indexData.messageRolling,
      tabBar:tabBar.tabBar,//tabBar
      inviteHomedata: inviteHomedata.inviteHomedata//团长信息
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var tabBar = this.data.tabBar;
    tabBar[0].select = true;
    tabBar[1].select = false;
    tabBar[2].select = false;
    this.setData({
      tabBar: tabBar
    })
  },

  // 页面渲染 调用
  onShow: function () {
    var that = this;

    if (that.data.navbarInitTop == 0) {
      //获取节点距离顶部的距离销量价格
      wx.createSelectorQuery().select('#navbar').boundingClientRect(function (rect) {
        if (rect && rect.top > 0) {
          var navbarInitTop = parseInt(rect.top);
          that.setData({
            navbarInitTop: navbarInitTop
          });
        }
      }).exec();
    }
    // 20200221分类
    if (that.data.classIfIdTop == 0) {
      //获取节点距离顶部的距离
      wx.createSelectorQuery().select('#classIfIdTop').boundingClientRect(function (rect) {
        if (rect && rect.top > 0) {
          var classIfIdTop = parseInt(rect.top);
          that.setData({
            classIfIdTop: classIfIdTop
          });
        }
      }).exec();
    }

  },
  /**
   * 监听页面滑动事件
   */
  onPageScroll: function (e) {
    var that = this;
    var scrollTop = parseInt(e.scrollTop); //滚动条距离顶部高度
    //判断'滚动条'滚动的距离 和 '元素在初始时'距顶部的距离进行判断
    var isSatisfy = scrollTop >= that.data.navbarInitTop ? true : false;
    //为了防止不停的setData, 这儿做了一个等式判断。 只有处于吸顶的临界值才会不相等
    if (that.data.isFixedTop === isSatisfy) {
      return false;
    }
    that.setData({
      isFixedTop: isSatisfy,
      flag: isSatisfy
    });
  },
  shopBtn: function () {
    // 购物车添加
    num++;
    this.setData({
      shopSumText: num
    })
    app.globalData.ee = num;
  },
  mySort1: function (e) {
    // 价格
    //property 根据什么排序
    var property = e.currentTarget.dataset.property;
    var self = this;
    var arr = self.data.usedata;
    sortRule2 = true;
    sortClick2 = 1;
    this.setData({
      sortClick2: sortClick2
    })
    sortClick2 = 2;
    if (sortClick1 == 1) {
      sortRule1 = true;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule1))
      });
      sortRule1 = true;
    } else if (sortClick1 == 2) {
      sortRule1 = true;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule1))
      });
    } else if (sortClick1 == 3) {
      sortRule1 = false;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule1))
      });
    }
    this.setData({
      sortClick1: sortClick1
    })
    console.log(sortClick1)
    sortClick1++;
    if (sortClick1 > 3) {
      sortClick1 = 1;
    }
    // 点击排序
    var that = this;
    var isFixedTop = that.data.isFixedTop; //isFixedTop是一个开关变量，用来控制点击滑动到指定位置
    var query = wx.createSelectorQuery(); //获取某个元素返回的是一个对象
    if (isFixedTop) {
      var _heightd = this.data.navbarInitTop;
      wx.pageScrollTo({
        scrollTop: _heightd, //页面滚动的距离
        duration: 300, //页面滚动速度 单位ms
        success: function (e) { //成功函数
          that.setData({
            flag: false
          })
        }
      });
    }
  },
  mySort2: function (e) {
    // 销量
    //property 根据什么排序
    var property = e.currentTarget.dataset.property;
    var self = this;
    var arr = self.data.usedata;
    sortRule1 = true;
    sortClick1 = 1;
    this.setData({
      sortClick1: sortClick1
    })
    sortClick1 = 2;
    if (sortClick2 == 1) {
      sortRule2 = true;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule2))
      });
      sortRule2 = true;
    } else if (sortClick2 == 2) {
      sortRule2 = false;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule2))
      });
    } else if (sortClick2 == 3) {
      sortRule2 = true;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule2))
      });
    }
    this.setData({
      sortClick2: sortClick2
    })
    sortClick2++;
    if (sortClick2 > 3) {
      sortClick2 = 1;
    }
    console.log(sortClick2)
    // 点击排序
    var that = this;
    var isFixedTop = that.data.isFixedTop; //isFixedTop是一个开关变量，用来控制点击滑动到指定位置
    var query = wx.createSelectorQuery(); //获取某个元素返回的是一个对象
    if (isFixedTop) {
      var _heightd = this.data.navbarInitTop;
      wx.pageScrollTo({
        scrollTop: _heightd, //页面滚动的距离
        duration: 300, //页面滚动速度 单位ms
        success: function (e) { //成功函数
          that.setData({
            flag: false
          })
        }
      });
    }

  },
  compare: function (property, bol) {
    // 排序计算
    return function (a, b) {
      var value1 = a[property];
      var value2 = b[property];
      if (bol) {
        return value1 - value2;
      } else {
        return value2 - value1;
      }
    }

  },
  twoBtn: function () {
    twoBtn = !twoBtn;
    this.setData({
      twoBtn: twoBtn
    })
  },
  goIndexFenLei: function (event) {
    var newsid = event.currentTarget.dataset.newsid;
      wx.navigateTo({
        // url: '../indexDetails/indexDetails?newsid=' + newsid
        url: '../index2/index2?newsid=' + newsid 
        // 跳转到商品分类
      })
  },
  onShareAppMessage: function (res) {//20200313转发
    return {
      title: '自定义',
      path: '这里写你这个页面的路径',
      imageUrl: '../images/inviteHome/dancigou.png',
      success: function (shareTickets) {
        console.info(shareTickets + '成功');
        // 转发成功
      },
      fail: function (res) {
        console.log(res + '失败');
        // 转发失败
      },
      complete: function (res) {
        // 不管成功失败都会执行
      }
    }
  },
  goTabBar:function(event) {
    var newsid = event.currentTarget.dataset.newsid;
    wx.redirectTo({
      url: newsid
      // 跳转tabBar
    })
  },
  goDefaultHead: function () {//跳到默认团长
    wx.navigateTo({
      url: '../defaultHead/defaultHead'
    })
  },
  add: function (e) {
    // 加
    var count = this.data.usedata;
    var index = e.currentTarget.dataset.index;
    count[index].countNum++;
    count[index].minusBtn=false;
    this.setData({
      usedata: count
    });

    // this.countSum()
  },
  minus: function (e) {
    // 减
    var count = this.data.usedata;
    var index = e.currentTarget.dataset.index;
    if (count[index].countNum <=1) {
      count[index].countNum = 0;
      count[index].minusBtn = true;
    } else {
      count[index].countNum--;
      count[index].minusBtn = false;
    }
    this.setData({
      usedata: count
    })
    // this.countSum()
  },
  goClassifSellers: function (event) {
    var page = this;
    if (page.data.currentTab == event.currentTarget.dataset.current) {
      return false;
    } else {
      page.setData({
        currentTab: event.currentTarget.dataset.current
      });
    }
  },
  goIndexDetails:function(event){
    var newsid = event.currentTarget.dataset.newsid;
    wx.navigateTo({
      url: '../../../pages/details/details?newsid=' + newsid
      // 跳转到商品详情
    })
  },
  //弹框图显示，tabBar显示
  elasticFrameHide: function () {
    var elasticFrameBtn = this.data.elasticFrameBtn;
    elasticFrameBtn = true;
    this.setData({
      elasticFrameBtn: elasticFrameBtn
    })
    wx.showTabBar();//显示tabBar
  }
})